package com.ak.ty.service.impl;

import com.ak.ty.dao.User;
import com.ak.ty.entity.UserMapper;
import com.ak.ty.service.TyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TyServiceImpl implements TyService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    UserMapper userMapper;


    /**
     * 添加用户
     *
     * @param id
     * @param password
     * @param email
     * @return
     */
    @Override
    public Boolean addUser(String id, String password, String email) {
        User user = new User();
        user.setId(id);
        user.setPassword(password);
        user.setEmail(email);
        Integer result = userMapper.insert(user);
        return result > 0;
    }

    /**
     * 发送邮件
     *
     * @param body
     * @param to
     * @return
     */
    @Override
    public Boolean email(String body, String to) {
        //创建邮件正文
        String from = "203462009@qq.com";
        String subject = "信息";
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        try {
            mailSender.send(message);
            log.info("简单邮件已经发送。");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送简单邮件时发生异常！", e);
        }
        return false;
    }
}
